//
//  SelectManyView.m
//  EducationAdminClient
//
//  Created by KWSD on 16/1/11.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "SelectManyView.h"
#import "OneSelectCell.h"
#import "Masonry.h"
#import "UIColor+CL.h"


@interface SelectManyView ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,assign)NSInteger indexPathNum;
@property(nonatomic,strong)NSMutableArray *numArr;
@property(nonatomic,strong)NSMutableArray *btnArr;
@end

@implementation SelectManyView

-(instancetype)initWithFrame:(CGRect)frame{
    self =[super initWithFrame:frame];
    if(self){
        _numArr =[NSMutableArray array];
        _btnArr =[NSMutableArray array];
        UIView *v =[[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:v];
        self.backgroundColor = [UIColor whiteColor];
        _fram = frame;
        
        self.layer.cornerRadius = 8;
        v.backgroundColor =[UIColor clearColor];
        v.layer.cornerRadius = 10;
        v.layer.masksToBounds = YES;
        _tb =[[UITableView alloc]init];
        _tb.frame = CGRectMake(1, 1, frame.size.width-2, frame.size.height-48);
        [v addSubview:_tb];
        v.userInteractionEnabled = YES;
        _tb.dataSource = self;
        _tb.delegate = self;
        _tb.bounces = NO;
        self.userInteractionEnabled = YES;
        _tb.userInteractionEnabled = YES;
        _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
                _leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, frame.size.height-49+1, frame.size.width/2 - 0.5, 48)];
        //        [self addSubview:_leftBtn];
                [_leftBtn setTitle:@"取消" forState:UIControlStateNormal];
//                [_leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                _leftBtn.frame = CGRectMake(0, frame.size.height-49+1, frame.size.width/2 - 0.5, 48);
                _leftBtn.layer.borderColor = [[UIColor clearColor]CGColor];
                _leftBtn.layer.borderWidth = 1;
                _leftBtn.backgroundColor =[UIColor whiteColor];
                _leftBtn.layer.cornerRadius = 6;
                [_leftBtn setTitleColor:[UIColor colorWithHex:0x00a8fd] forState:UIControlStateNormal];
        
//                [v addSubview:_leftBtn];
        
                _rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width/2 +0.5, frame.size.height-49+1, frame.size.width/2 - 0.5, 48)];
        //        [self addSubview:_rightBtn];
                [_rightBtn setTitle:@"确定" forState:UIControlStateNormal];
                [_rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//                _rightBtn.frame = CGRectMake(frame.size.width/2 +0.5, frame.size.height-49+1, frame.size.width/2 - 0.5, 48);
                _rightBtn.layer.borderColor = [[UIColor clearColor]CGColor];
                _rightBtn.layer.borderWidth = 1;
                _rightBtn.backgroundColor =[UIColor whiteColor];
                _rightBtn.layer.cornerRadius = 6;
                [_rightBtn setTitleColor:[UIColor colorWithHex:0x00a8fd] forState:UIControlStateNormal];
        
//                [v addSubview:_rightBtn];
        
//                UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, frame.size.height-49, _leftBtn.frame.size.width + _rightBtn.frame.size.width, 1)];
//                [self addSubview:imgView];
//                imgView.backgroundColor = [UIColor colorWithHex:0xd5d5d5];
//        
//                UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(_leftBtn.frame.origin.x + _leftBtn.frame.size.width, frame.size.height-49+1, 1, frame.size.height - (frame.size.height-49+1))];
//                [self addSubview:imgView1];
//                imgView1.backgroundColor = [UIColor colorWithHex:0xd5d5d5];
        
//        [_leftBtn addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
//        [_rightBtn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];

        [_leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        self.selectManyArr = [[NSMutableArray alloc]init];
        
    }
    return self;
}

-(void)tapd{
    NSLog(@"33");
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 48;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *vv =[[UIView alloc]init];
    vv.backgroundColor =[UIColor grayColor];
    
    _leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, _fram.size.width/2 - 0.5, 48)];
    [vv addSubview:_leftBtn];
    [_leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    _leftBtn.layer.borderColor = [[UIColor clearColor]CGColor];
    _leftBtn.layer.borderWidth = 1;
    _leftBtn.backgroundColor =[UIColor whiteColor];
    _leftBtn.layer.cornerRadius = 6;
    [_leftBtn setTitleColor:[UIColor colorWithHex:0x00a8fd] forState:UIControlStateNormal];
    
    _rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(_fram.size.width/2 +0.5, 0, _fram.size.width/2 - 0.5, 48)];
    //        [self addSubview:_rightBtn];
    [_rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //                _rightBtn.frame = CGRectMake(frame.size.width/2 +0.5, frame.size.height-49+1, frame.size.width/2 - 0.5, 48);
    _rightBtn.layer.borderColor = [[UIColor clearColor]CGColor];
    _rightBtn.layer.borderWidth = 1;
    _rightBtn.backgroundColor =[UIColor whiteColor];
    _rightBtn.layer.cornerRadius = 6;
    [_rightBtn setTitleColor:[UIColor colorWithHex:0x00a8fd] forState:UIControlStateNormal];
    
    [vv addSubview:_rightBtn];
    
    //                UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, frame.size.height-49, _leftBtn.frame.size.width + _rightBtn.frame.size.width, 1)];
    //                [self addSubview:imgView];
    //                imgView.backgroundColor = [UIColor colorWithHex:0xd5d5d5];
    //
    //                UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(_leftBtn.frame.origin.x + _leftBtn.frame.size.width, frame.size.height-49+1, 1, frame.size.height - (frame.size.height-49+1))];
    //                [self addSubview:imgView1];
    //                imgView1.backgroundColor = [UIColor colorWithHex:0xd5d5d5];
    
            [_leftBtn addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
            [_rightBtn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    

    
    vv.userInteractionEnabled = YES;
    return vv;
}

-(void)tapKK{
    NSLog(@"e44");
}

-(void)btnClick:(UIButton *)btn{
    NSLog(@"44444");
}

#pragma mark - leftClick
-(void)leftClick{
    NSLog(@"left");
    if(self.delegateView)[self.delegateView performSelector:@selector(tapClick1) withObject:nil];
}

-(void)rightClick{
    NSLog(@"right");
    NSMutableArray *arr1 = [[NSMutableArray alloc]init];
    NSMutableArray *arr2 = [[NSMutableArray alloc]init];

    for (NSDictionary *dic in _selectManyArr) {
        [arr1 addObject:[dic valueForKey:@"DIC_NAME"]];
        [arr2 addObject:[dic valueForKey:@"DIC_ID"]];

    }
    self.seleTitle = [arr1 componentsJoinedByString:@","];
    self.seleTitleNum = [arr2 componentsJoinedByString:@","];
    
        if (self.delegateView!=nil) {
            [self.delegateView performSelector:@selector(rightBtnClick1:) withObject:nil];
        }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"identifier";
    OneSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if(cell==nil){
        cell = [[OneSelectCell alloc]initIdentifier:str];
    }
    NSDictionary *dic =[_dataArr objectAtIndex:indexPath.row];
    cell.la.text = [dic objectForKey:@"DIC_NAME"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.btn.tag = indexPath.row+1001;
    [cell.btn setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
    [_btnArr addObject:cell.btn];
    if(_numArr.count>0){
        for(NSNumber *num in _numArr){
            NSInteger nums = [num integerValue];
            if(cell.btn.tag==nums){
                [cell.btn setImage:[UIImage imageNamed:@"形状-20-拷贝"] forState:UIControlStateNormal];
                cell.btn.selected = YES;
            }
            
        }
  
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIButton *btn = (UIButton *)[self viewWithTag:1001+indexPath.row];
    //    if(_indexPathNum>0){
    //    UIButton *btn2 = (UIButton *)[self viewWithTag:_indexPathNum];
    //   }
    
    NSDictionary *dic =[_dataArr objectAtIndex:indexPath.row];

    if(btn.selected){
        [btn setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
        btn.selected = NO;
        [_selectManyArr removeObject:dic];
        NSNumber *number = [NSNumber numberWithInt:btn.tag];
        [_numArr removeObject:number];

    }
    else{
        [btn setImage:[UIImage imageNamed:@"形状-20-拷贝"] forState:UIControlStateNormal];
        btn.selected =YES;
        
        [_selectManyArr addObject:dic];

//        if(_indexPathNum>0&&btn.tag!=_indexPathNum){
//            UIButton *btn2 = (UIButton *)[self viewWithTag:_indexPathNum];
//            [btn2 setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
//            btn2.selected = NO;
//        }
        _indexPathNum=btn.tag;
        NSNumber *number = [NSNumber numberWithInt:btn.tag];
        [_numArr addObject:number];
//        NSDictionary *dic =[_dataArr objectAtIndex:indexPath.row];
        
//        [self.selectManyArr removeObject:dic];
//        _seleTitle = [dic objectForKey:@"DIC_NAME"];
//        _seleTitleNum =[NSString stringWithFormat:@"%@",[dic objectForKey:@"DIC_ID"]];
    }
//    if (self.delegateView!=nil) {
//        [self.delegateView performSelector:@selector(rightBtnClick:) withObject:nil];
//    }
}


-(void)setDataArr:(NSArray *)dataArr{
    NSLog(@"~~~%ld",(long)_indexPathNum);
    if(_numArr.count>0){
        for(UIButton *btn in _btnArr){
            for(NSNumber *num in _numArr){
                NSInteger nums = [num integerValue];
                if(btn.tag==nums){
                    [btn setImage:[UIImage imageNamed:@"形状-20-拷贝"] forState:UIControlStateNormal];
                    btn.selected = YES;
                }
               
            }
        }
        
    }
    _dataArr = dataArr;
    [_tb reloadData];
}


@end
